Android中SharedPreferences.Editor的apply()与commit()方法

写在前面

  今天在写 LifeTips App 时,涉及到存储用户调节的屏幕亮度时,使用了 SharedPreferences,及 SharedPreferences.Editor,结果 Android Studio 出现了”黄色的感叹号“(本人有强迫症,不喜欢任何 warning。。),提示:Consider using apply() instead of commit on SharedPreferences. Whereas commit blocks and writes its data to persistent storage immediately, apply will handle it in the background. 所以就将 editor.commit(); 改为 editor.apply()了。

  LifeTips App GitHub源码链接:https://github.com/selfconzrr/LifeTips

  知其然就要知其所以然:看官方文档

commit()的文档

  官方文档如下:

  Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

  Note that when two editors are modifying preferences at the same time, the last one to call commit wins.

  If you don’t care about the return value and you’re using this from your application’s main thread, consider using apply() instead.

apply()的文档

  官方文档如下:

  Commit your preferences changes back from this Editor to the SharedPreferences object it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

  Note that when two editors are modifying preferences at the same time, the last one to call apply wins.

  Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won’t be notified of any failures. If another editor on this SharedPreferences does a regular commit() while a apply() is still outstanding, the commit() will block until all async commits are completed as well as the commit itself.

  As SharedPreferences instances are singletons within a process, it’s safe to replace any instance of commit() with apply() if you were already ignoring the return value.

  You don’t need to worry about Android component lifecycles and their interaction with apply() writing to disk. The framework makes sure in-flight disk writes from apply() complete before switching states.

文档说明

  需要注意的是 commit() 方法是 Added in API level 1 的,也就是 sdk1 就已经存在了。而 apply() 方法是 Added in API level 9 的。

  1. commit() 有返回值,成功返回 true,失败返回 false。而 apply() 没有返回值。
  2. commit() 方法是同步提交到硬件磁盘,因此,在多个并发的提交 commit 的时候,他们会等待正在处理的 commit 保存到磁盘后在操作,从而降低了效率。
  3. apply() 是将修改的数据提交到内存,而后异步真正的提交到硬件磁盘。
为什么建议使用apply()替代commit() ?

  因为 Android 的设计人员发现,开发人员对 commit() 的返回值不感兴趣,而且在数据并发处理时使用 commit() 要比 apply() 效率低,所以推荐使用apply()。

------至所有正在努力奋斗的程序猿们!加油!!
有码走遍天下 无码寸步难行
1024 - 梦想,永不止步!
爱编程 不爱Bug
爱加班 不爱黑眼圈
固执 但不偏执
疯狂 但不疯癫
生活里的菜鸟
工作中的大神
身怀宝藏,一心憧憬星辰大海
追求极致,目标始于高山之巅
一群怀揣好奇,梦想改变世界的孩子
一群追日逐浪,正在改变世界的极客
你们用最美的语言,诠释着科技的力量
你们用极速的创新,引领着时代的变迁

——乐于分享,共同进步,欢迎补充
——Any comments greatly appreciated
——诚心欢迎各位交流讨论!QQ:1138517609
——GitHub:https://github.com/selfconzrr

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BugFree_张瑞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值